const app1 = new Vue({
    el: '#app1',
    data: {
        a: [],
        username: 'Candy',
        author: 'author',
        show_header: true,
        is_login: false
    },
    created(){
        console.log("初始化")
        var aid = localStorage.getItem("aid");
        var a_type = localStorage.getItem("a_type");
        // 展示列表
        fetch('/article/new_list?a_type=' + a_type)
        .then(resp => resp.json())
        .then(json => {
            if(json.code==200){
                this.a = json.results.message
            }
        });
        // 结束
        fetch('/article/detail?aid=' + aid)
        .then(resp => resp.json())
        .then(json => {
            if(json.code==200){
                this.tableData = json.results.message
                if(document.getElementById('show-editor')){
                    document.getElementById('show-editor').innerHTML = `<textarea id="message" name="message">` + json.results.message + `</textarea>`
                    document.getElementById('title').innerHTML = json.results.title
                    this.author = json.results.name
                    var showEditor
                    $(function() {
                        showEditor = editormd.markdownToHTML("show-editor", {
                            width  : "80%",
                            height : 800,
                            editor: false,
                            watch: true,
                            preview : true,
                            path: "/static/lib/"
                        });
                    });
                }else{
                    console.log(11)
                }
            }else{
                alert(json.results.message)
            }
        })
        setTimeout(function(){
            var imgsize = $('body img').length; //得出img标签的总数量
            console.log(imgsize)
            for (i = 0; i < imgsize; i++) {
                //如果当前img标签的alt属性不存在，则添加
                if (!$('body img').eq(i).attr('alt')) {
                    $('body img').eq(i).attr('width', "70%")
                    $('body img').eq(i).attr('align', "center")
                }
            }
        },1000)
    },
    methods:{
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        add(row, a_type){
            this.$message(row);
            console.log(row)
            localStorage.setItem('aid', row)
            localStorage.setItem('a_type', a_type)
            location.href = '/static/templates/new_detail.html?aid=' + Math.random()
        },
        show(){
            fetch('/article/new_list')
            .then(resp => resp.json())
            .then(json => {
                if(json.code==200){
                    this.a = json.results.message
                    this.$notify({
                        title: '刷新成功',
                        message: '获取最新' + this.a.length + '条数据',
                        type: 'success',
                        offset: 100
                    });
                    return
                }
            })
        },
        return_ref(){
            location.href = '/static/templates/index.html'
        },
        editor_again(){
            this.$message({
                showClose: true,
                message: '再编辑接口暂未开放 请登录后移步至个人中心进行操作',
                type: 'warning'
            });
        },
        share(){
            this.$message({
                showClose: true,
                message: '分享接口暂未开放 请登录后移步至个人中心进行操作',
                type: 'warning'
            });
        },
        delete_(){
            this.$message({
                showClose: true,
                message: '删除接口暂未开放 请登录后移步至个人中心进行操作',
                type: 'warning'
            });
        },
    }
});

function shuffleSelf(array, size) {
    var index = -1,
        length = array.length,
        lastIndex = length - 1;

    size = size === undefined ? length : size;
    while (++index < size) {
        // var rand = baseRandom(index, lastIndex),
        var rand = index + Math.floor( Math.random() * (lastIndex - index + 1))
            value = array[rand];

        array[rand] = array[index];

        array[index] = value;
    }
    array.length = size;
    return array;
}
